<?php

namespace Fluency\Exceptions;

use Fluency\Dispatcher;
use Fluency\Request;

/**
 * @package Fluency\Exceptions
 * @author  Ignas Rudaitis
 * @version 2.0.0
 */
class ForwardException extends \RuntimeException implements InterruptException
{
    private $request;

    public function __construct(Request $req)
    {
        $this->request = $req;
    }

    public static function toAction($action, array $params = array())
    {
        return new self(Request::forAction($action)->withParameters($params));
    }

    /**
     * @return Fluency\Response
     */
    public function getResponse(Dispatcher $dispatcher, Request $previous)
    {
        return $dispatcher->dispatch($this->request->preferringFormat($previous->getFormat()));
    }
}